package com.graduation.smartparkingplatform.Controller;

import com.github.pagehelper.PageInfo;
import com.graduation.smartparkingplatform.Pojo.Distinguish;
import com.graduation.smartparkingplatform.Response.Response;
import com.graduation.smartparkingplatform.Response.ResponseUtils;
import com.graduation.smartparkingplatform.Service.DistinguishService;
import com.graduation.smartparkingplatform.Utils.ExcelUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;

@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/distinguish")
public class DistinguishController {

  @Autowired DistinguishService distinguishService;

  // 获取识别记录列表
  @GetMapping("/distinguishList")
  public Response getCompanyList(
      @RequestParam(value = "page", defaultValue = "1") Integer page,
      @RequestParam(value = "count", defaultValue = "10") Integer count) {
    try {
      PageInfo<Distinguish> distinguishList = distinguishService.getdistinguishList(page, count);
      return ResponseUtils.buildSuccess(distinguishList);
    } catch (Exception e) {
      return ResponseUtils.buildError(e);
    }
  }

  // 模糊搜索
  @GetMapping("/searchDistinguish")
  public Response searchDistinguish(
      @RequestParam(value = "page", defaultValue = "1") Integer page,
      @RequestParam(value = "count", defaultValue = "10") Integer count,
      @RequestParam(value = "distinguishType", defaultValue = "null") String distinguishType,
      @RequestParam(value = "carPlate", defaultValue = "null") String carPlate,
      @RequestParam(value = "distinguishTime", defaultValue = "null") String distinguishTime) {
    try {
      PageInfo<Distinguish> searchList =
          distinguishService.searchDistinguish(
              page, count, distinguishType, carPlate, distinguishTime);
      return ResponseUtils.buildSuccess(searchList);
    } catch (Exception e) {
      return ResponseUtils.buildError(e);
    }
  }

  // 删除识别记录
  @PostMapping("/deleteDistinguish")
  public Response deleteDistinguish(@RequestParam("DistinguishId") Integer DistinguishId) {
    Boolean delete = distinguishService.deleteDistinguish(DistinguishId);
    if (delete) {
      return ResponseUtils.buildSuccess("删除识别记录成功");
    }
    return ResponseUtils.buildError("删除识别记录失败");
  }

  // 导出Excel
  @RequestMapping("/exportDistinguishExcel")
  public void exportDistinguishExcel(HttpServletResponse response) {

    List<Distinguish> distinguishList = distinguishService.getdistinguishListAll();
    // 获取当前时间
    Calendar calendar = Calendar.getInstance();
    // 定义时间格式化
    SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd_HH:mm:ss");
    String nowTime = formatter.format(calendar.getTime());
    // 导出操作
    ExcelUtils.exportExcel(
        distinguishList,
        "识别记录",
        "sheet1",
        Distinguish.class,
        "distinguishDATA" + nowTime + ".xls",
        response);
    log.info("exportDistinguishExcel");
  }
}
